<?php

	final class SmRoot0v0r01_group extends _smuoiDbRow implements SmRoot_group{
		
		protected $table = 'groups';

		protected $get_functions = 'name';
		protected $set_functions = 'name,members';
		
		private $members;
		
		function primary(){
			return (substr($this->data->nam, 0, 1) == ':') ? substr($this->data->nam, 1) : false;
		}
		
		function accept_member($u){
			return ((!$uid = $this->primary())||($u->id == $uid));	
		}
		
		function members(){
			if(!is_null($this->members)) return $this->members;
			if($uid = $this->primary()) return $this->members = array($uid);
			if(!$mq = $this->pkg->al()->execute("SELECT `id` FROM `users` WHERE `groups` LIKE '%".$this->id."%';")) return $this->members = array();
			foreach($mq as $m) $this->members[$m['id']] = $m['id'];
			return $this->members;
		}
		
		protected function set_members($ma){
			if($this->primary()) return false;
			if(!is_array($ma)) $ma = explode(',', $ma);
			$cur = $this->members();
			foreach($ma as $id){
				if(!$usr = $this->pkg->db->get($id)) continue;
				if(!$usr->join_group($this)) continue;
				$usr->commit();
				if(isset($cur[$id])) unset($cur[$id]);
			}
			foreach($cur as $id){
				if(!$usr = $this->pkg->db->get($id)) continue;
				$usr->leave_group($this);
				$usr->commit();
			}
			return true;
		}
				
		function name(){
			return ($uid = $this->primary()) ? smdb($uid)->name : $this->data->nam;
		}
		
		function set_name($n){
			if($this->primary()) return false;
			if(!$n = preg_replace('/[^A-Za-z]/', '', $n)) return false;
			if($n == $this->data->nam) return false;
			if($this->pkg->al()->exists($this->table(), array('nam'=>$n))) return false;
			$this->hard_set('nam', $n);
			return true;
		}
	}


?>